#!/bin/bash
#
# Just create an image in the filesystem, then grow it.

set -e

TEMP_D=""

rq() {
   local out="${TEMP_D}/out"
	"$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
}

cleanup() {
	[ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
trap cleanup EXIT

img="${TEMP_D}/disk.img"
mp="${TEMP_D}/mp"

size=1000M
osize=500M
rm -f $img

truncate -s $osize "$img"

label_flag="--label=${PT_TYPE:-dos}"
echo "2048," | rq sfdisk $label_flag --force --unit=S "$img"

truncate -s "$size" "$img"

echo "==== before ===="
sfdisk --list --unit=S "$img"

err="${TEMP_D}/gp.err"
out="${TEMP_D}/gp.out"
if ! growpart -v -v "$img" 1 2>"$err" > "$out"; then
    cat "$err" "$out"
    echo "failed"
    exit 1
fi
echo "==== growpart-stderr ==="
cat "$err"
echo "==== growpart-stdout ===="
cat "$out"
grep -q "^CHANGED:" "$out" ||
   { echo "did not find 'CHANGED'"; exit 1; }

echo "==== after ===="
sfdisk --list --unit=S "$img"
